const user = require("../sql/user");
const sql = require("../sql/index");

// 每次登录状态初始化为0
sql.set(user, { isLogin: 1 }, { isLogin: 0 }, d => {
    // console.log(d)
})
exports.logins = function () {
    return (req, res, next) => {
        sql.get(user, {}, data => {
            // console.log(data[0].tel)
            // console.log(req.query.tel)
            if (data.length > 0) {
                if (data[0].tel == req.query.tel) {
                    req.session.userid = data[0].userid;
                    sql.set(user, {
                        tel: req.query.tel,
                    }, {
                        isLogin: 1
                    }, () => {
                        // 修改数据集合完成后，再提示登录成功
                        res.send(JSON.stringify({
                            code: 1,
                            msg: "登录成功",
                            age: data[0].userid,
                        }));
                    })
                } else {
                    res.send(JSON.stringify({
                        code: 2,
                        msg: "登录失败，手机号不对"
                    }));
                }
            } else {
                res.send(JSON.stringify({
                    code: 0,
                    msg: "登录失败，用户不存在"
                }));
            }
        })

    }
}
